﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZilLion.Core.Unities.UnitiesMethods;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.Log;
using ZilLion.Service.WeChatMall.Domain.Entitys.AppInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.AppInfo;
using ZilLion.Service.WeChatMall.Domain.Responses.Appinfo;
using ZilLion.Service.WeChatMall.Domain.Services.Interface;

namespace ZilLion.Service.WeChatMall.Domain.Services
{
    public class AppInfoService : IAppInfoService
    {
        private readonly IAppBasicInfoReponsitory _appBasicInfoReponsitory;
        private readonly IAppGsRelationShipReponsitory _appGsRelationShipReponsitory;
        private readonly IAppRaInfoReponsitory _appRaInfoReponsitory;
        private readonly IAppShopRelationShipReponsitory _appShopRelationShipReponsitory;

        public AppInfoService(IAppBasicInfoReponsitory appBasicInfoReponsitory,
            IAppRaInfoReponsitory appRaInfoReponsitory, IAppGsRelationShipReponsitory appGsRelationShipReponsitory,
            IAppShopRelationShipReponsitory appShopRelationShipReponsitory)
        {
            _appBasicInfoReponsitory = appBasicInfoReponsitory;
            _appRaInfoReponsitory = appRaInfoReponsitory;
            _appGsRelationShipReponsitory = appGsRelationShipReponsitory;
            _appShopRelationShipReponsitory = appShopRelationShipReponsitory;
        }

        public IList<AppInfoDto> GetApps(string zlcompanykey)
        {
            IList<AppInfoDto> result = new List<AppInfoDto>();
            var basicinfos = _appBasicInfoReponsitory.GetAppsByKey(zlcompanykey);
            foreach (var info in basicinfos)
            {
                var rainfo = _appRaInfoReponsitory.GetRaInfo(info.AppId);
                result.Add(new AppInfoDto
                {
                    AppBasicInfo = info,
                    AppRaInfo = rainfo ?? new AppRaInfoEntity()
                });
            }
            return result;
        }

        public AppInfoDto SaveAppInfo(AppInfoDto dto)
        {
            var basic = dto.AppBasicInfo;
            var rainfo = dto.AppRaInfo;
            using (var unit = new UnitOfWork())
            {
                _appBasicInfoReponsitory.RemoveInfo(basic, unit);
                _appBasicInfoReponsitory.AddInfo(basic, unit);
                if (rainfo != null)
                {
                    _appRaInfoReponsitory.RemoveInfo(rainfo, unit);
                    _appRaInfoReponsitory.AddInfo(rainfo, unit);
                }
                unit.Complete();
            }
            return dto;
        }

        public AppInfoDto GetAppByAppId(string appid)
        {
            var basic = _appBasicInfoReponsitory.GetAppsByAppid(appid);
            var rainfo = _appRaInfoReponsitory.GetRaInfo(appid);
            return basic == null ? null : new AppInfoDto {AppRaInfo = rainfo, AppBasicInfo = basic};
        }

        public void DeleteApp(string appid)
        {
            var basic = _appBasicInfoReponsitory.GetAppsByAppid(appid);
            var rainfo = _appRaInfoReponsitory.GetRaInfo(appid);
            using (var unit = new UnitOfWork())
            {
                _appBasicInfoReponsitory.RemoveInfo(basic, unit);
                _appRaInfoReponsitory.RemoveInfo(rainfo, unit);
                _appGsRelationShipReponsitory.RemoveRelationShipByAppId(appid, unit);
                _appShopRelationShipReponsitory.RemoveRelationShipByAppId(appid, unit);
                unit.Complete();
            }
        }

        public void SaveAppGsRelationShip(IList<AppGsRelationShipEntity> list)
        {
            if (!list.Any()) return;
            using (var unit = new UnitOfWork())
            {
                _appGsRelationShipReponsitory.RemoveRelationShipByAppId(list.FirstOrDefault()?.AppId, unit);
                foreach (var entity in list)
                    _appGsRelationShipReponsitory.AddRelationShip(entity, unit);

                unit.Complete();
            }
        }

        public bool CheckAppIdValid(string appid, string crmurl)
        {
            var checkurl = $"{crmurl}/Api/Config/CheckAppid?appid={appid}";
            var result = false;
            try
            {
                result = (HttpHelper.HttpRequestGet<CrmCheckAppIdValidResponse>(checkurl)?.checkResult ??
                          string.Empty.ToLower()) == "same";
            }
            catch (Exception e)
            {
                ZilLionLogManager.Instance().Error(e);
            }
            return result;
        }
    }
}